package {
	import fl.video.FLVPlayback;
	import fl.video.VideoAlign;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	import src.component.UIScrollBar;
	import src.event.EventAnj;
	import src.info.LessonInfo;
	import src.info.LetterInfo;
	import src.manager.FilterManager;
	import src.manager.SoundManager;

	/**
	 * 課件 PMI Tool
	 * @author Anjue
	 */
	 [SWF(width="1024",height="768")]
	public class PMI extends Sprite
	{
//		[Embed(source="resource/font/Pinyinok.ttf",fontFamily="Regular")]
//		private var embeddedFont:String;
		
		private var  URL:String = "";
		
		private var main:PMI_MainPanel;
		private var videoPlayer:FLVPlayback;
		
		private var now_type:uint = 0;
		
		/** 所有聲母 **/
		private var allSheng:Array = ["b","p","m","f","d","t","n","l","g","k","h","j","q","x","zh","ch","sh","r","z","c","s","y","w"];
		
		/** 所有韻母 **/
		private var allYun:Array = ["a","o","e","i","u","ü","ê","er","ai","ei","ao","ou","an","en","ang","eng","ong","ia","ie","iao","iu","ian","in","iang","ing","iong","ua","uo","uai","ui","uan","un","uang","ueng","üe","üan","ün"];
		
		/** 重點聲母索引  注：在所有聲母數組中的索引 **/
		private var importantSheng:Array = [];
		
		/** 重點韻母索引 注：在所有韻母數組中的索引**/
		private var importantYun:Array = ["ang","eng","ong","ing"];
		
		/** 選擇的重點聲母或韻母 **/
		private var selectedImportant:String = "";
		
		private var ergeShowBool:Boolean = false;
		
		private var title:TextField;
		
		/** 滾動條組件 **/
		private var _uiScorllBar:UIScrollBar;
		public function PMI()
		{
			init();
		}
		
		private function init():void
		{
			LessonInfo.ownEvent.addEventListener(Event.COMPLETE,lessonCom);
			LessonInfo.doLoad();
		}
		
		private function lessonCom(e:Event):void
		{
			URL = "resource/"+LessonInfo.lessonID+"/";
			importantSheng = LessonInfo.importantSheng;
			importantYun = LessonInfo.importantYun;
			LetterInfo.ownEvent.addEventListener(Event.COMPLETE,letterCom);
			LetterInfo.doLoad();
		}
		
		private function letterCom(e:Event):void
		{
			main = new PMI_MainPanel();
			main.myTitle.text = LessonInfo.title;
			setMyTextFormat(main.myTitle,"MYuenHK-Medium",32);
			this.addChild(main);
			initPMI(0);
		}
		
		private function initPMI($type:uint):void
		{
			now_type = $type;
			var i:uint = 0;
			for(i;i<4;i++)
			{
				if(i == now_type)
				{
					main["type_"+i].gotoAndStop(2);
					main["type_"+i].buttonMode = false;
					main["panel_"+i].visible = true;
				}else
				{
					main["type_"+i].gotoAndStop(1);
					main["type_"+i].buttonMode = true;
					main["panel_"+i].visible = false;
				}
				
				if(!main["type_"+i].hasEventListener(MouseEvent.CLICK))
					main["type_"+i].addEventListener(MouseEvent.CLICK,typeClick);
			}
			
			var list:Array = importantSheng.concat(importantYun);
			
			var str1:String;
			if(importantSheng.length>0 && importantYun.length>0)
			{
				str1 = "聲母、韻母";
			}else if(importantSheng.length>0 && importantYun.length==0)
			{
				str1 = "聲母";
			}else if(importantSheng.length==0 && importantYun.length>0)
			{
				str1 = "韻母";
			}
			str1 = "普通話知識：複習" + str1;
			
			var str:String = "";
			i=0;
			for(i;i<list.length;i++)
			{
				if(i == list.length-1)
					str += list[i] + "。";
				else
					str += list[i] + "、";
			}
			main.title.embedFonts = true;
			main.title.htmlText = "<font color='#008F45' size='22' face='MYUenHK-Medium'>"+str1+"</font>"+"<font color='#008F45' size='24' face='PinYinok'>"+str+"</font>";
			
			initPanelFunction(now_type);
		}
		
		private function typeClick(e:MouseEvent):void
		{
			var target:MovieClip = e.currentTarget as MovieClip;
			var type:uint = target.name.split("_")[1];
			clearPanelListener(now_type);
			initPMI(type);
		}
		
		/**
		 * 初始化面板功能
		 * @$type 面板類型  0.語音知識  1.拼讀/視讀  2.兒歌  3.練習
		 */
		private function initPanelFunction($type:uint):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var i:uint = 0;
			switch(now_type)
			{
				case 0:
					var video:MovieClip = panel.panel_duyin.video as MovieClip;
					if(!video.getChildByName("video_player"))
					{
						videoPlayer = new FLVPlayback();
						videoPlayer.name = "video_player";
						videoPlayer.registrationWidth = 310;
						videoPlayer.registrationHeight = 180;
						videoPlayer.align = VideoAlign.TOP_LEFT;
						video.addChild(videoPlayer);
					}
					panel.panel_duyin.tip.visible = false;
					panel.panel_duyin.btn_zhenren.gotoAndStop(1);
					panel.panel_duyin.btn_zhenren.addEventListener(MouseEvent.CLICK,zhenrenOrZuixing);
					panel.panel_duyin.btn_zuixing.gotoAndStop(1);
					panel.panel_duyin.btn_zuixing.addEventListener(MouseEvent.CLICK,zhenrenOrZuixing);
					i = 0;
					for(i;i<allSheng.length;i++)
					{
						if(!checkInList(allSheng[i],importantSheng))
						{
							panel["s_"+i].gotoAndStop(2);
							panel["s_"+i].alpha = 0.5;
						}else
						{
							panel["s_"+i].gotoAndStop(1);
							panel["s_"+i].addEventListener(MouseEvent.CLICK,shengOrYunClick);
						}
					}
					i = 0;
					trace(allYun.length)
					for(i;i<allYun.length;i++)
					{
						if(!checkInList(allYun[i],importantYun))
						{
							panel["y_"+i].gotoAndStop(2);
							panel["y_"+i].alpha = 0.5;
						}else
						{
							panel["y_"+i].gotoAndStop(1);
							panel["y_"+i].addEventListener(MouseEvent.CLICK,shengOrYunClick);
						}
					}
					i = 0;
					for(i;i<6;i++)
					{
						panel["tool_"+i].gotoAndStop(1);
						panel["tool_"+i].addEventListener(MouseEvent.CLICK,toolClick);
					}
					
					panel.panel_tool.show.show.gotoAndStop(1);
					panel.panel_tool.title.gotoAndStop(1);
					panel.panel_tool.visible = false;
					panel.panel_duyin.visible = true;
					
					initYunTips();
					break;
				case 1:
					var show:MovieClip = panel.show.show;
					show.y = 0;
					while(show.numChildren>0) show.removeChildAt(0);
					
					var list:Array = importantSheng.concat(importantYun);
					var str:String = "";
					if(importantSheng.length>0 && importantYun.length>0)
					{
						str = "聲母、韻母";
					}else if(importantSheng.length>0 && importantYun.length==0)
					{
						str = "聲母";
					}else if(importantSheng.length==0 && importantYun.length>0)
					{
						str = "韻母";
					}
					i = 0;
					var letterStr:String = "";
					for(i;i<list.length;i++)
					{
						if(i == list.length-1)
							letterStr += list[i];
						else
							letterStr += list[i] + "、";
						var item:Item_Important = new Item_Important();
						item.name = list[i];
						item.status = "s";
						item.btn_s.gotoAndStop(2);
						item.btn_s.addEventListener(MouseEvent.CLICK,shiOrPinClick);
						item.btn_p.gotoAndStop(1);
						item.btn_p.addEventListener(MouseEvent.CLICK,shiOrPinClick);
						item.y += (144+40)*(i);
						item.letter.text = list[i];
						setMyTextFormat(item.letter,"PinYinok",58);
						show.addChild(item);
						
						var lword:Array = LessonInfo.getInfoByLevelID(list[i]).lessonWord;
						var j:uint = 0;
						for(j;j<3;j++)
						{
							item["lesson_"+j].gotoAndStop(1);
							var lett:String = lword[j];
							if(lett)
							{
								item["lesson_"+j].letter.mouseEnabled = false;
								item["lesson_"+j].word.mouseEnabled = false;
								item["lesson_"+j].addEventListener(MouseEvent.CLICK,wordClick);
								item["lesson_"+j].letter.text = LetterInfo.getInfoByLevelID(lett).pin;
//								setMyTextFormat(item["lesson_"+j].letter,"Regular",22);
								item["lesson_"+j].word.text = LetterInfo.getInfoByLevelID(lett).example;
							}else
							{
								item["lesson_"+j].visible = false;
							}
						}
						
						var oword:Array = LessonInfo.getInfoByLevelID(list[i]).otherWord;
						j=0
						for(j;j<3;j++)
						{
							item["other_"+j].gotoAndStop(1);
							var let:String = oword[j];
							if(let)
							{
								item["other_"+j].addEventListener(MouseEvent.CLICK,wordClick);
								item["other_"+j].letter.mouseEnabled = false;
								item["other_"+j].word.mouseEnabled = false;
								item["other_"+j].letter.text = LetterInfo.getInfoByLevelID(let).pin;
//								setMyTextFormat(item["other_"+j].letter,"Regular",22);
								item["other_"+j].word.text = LetterInfo.getInfoByLevelID(let).example;
							}else
							{
								item["other_"+j].visible = false;
							}
						}
					}
					panel.title.htmlText = "<font color='#000000' size='24' face='MYUenHK-Medium'>"+str+"</font>"+"<font color='#000000' size='36' face='PinYinok'>"+letterStr+"</font>";
					_uiScorllBar = createScrollBar(panel);
					break;
				case 2:
					panel.show.show.gotoAndStop(1);
					ergeShowBool = false;
					
					var l:URLLoader = new URLLoader();
					l.addEventListener(Event.COMPLETE,loadErgeCom);
					l.load(new URLRequest(URL+LessonInfo.lessonID+"_PMI_t3_1.txt"));
					
					panel.txt.text = LessonInfo.ergeName;
					panel.btn_important.addEventListener(MouseEvent.CLICK,showImportant);
					panel.btn_play.addEventListener(MouseEvent.CLICK,playErge);
					panel.btn_stop.addEventListener(MouseEvent.CLICK,stopErge);
					panel.voice.bar.buttonMode = true;
					panel.voice.bar.addEventListener(MouseEvent.MOUSE_DOWN,voiceDown);
					stage.addEventListener(MouseEvent.MOUSE_UP,voiceUp);
					break;
				case 3:
					panel.show.show.gotoAndStop(1);
					panel.btn_lianxi.gotoAndStop(2);
					panel.btn_daan.gotoAndStop(1);
					panel.btn_lianxi.addEventListener(MouseEvent.CLICK,showLianxi);
					panel.btn_daan.addEventListener(MouseEvent.CLICK,showDaan);
					
					var lo:URLLoader = new URLLoader();
					lo.addEventListener(Event.COMPLETE,loadLianxiCom);
					lo.load(new URLRequest(URL+LessonInfo.lessonID+"_PMI_t4_1.txt"));
					
					var la:URLLoader = new URLLoader();
					la.addEventListener(Event.COMPLETE,loadDaanCom);
					la.addEventListener(IOErrorEvent.IO_ERROR,loadDaanErr);
					la.load(new URLRequest(URL+LessonInfo.lessonID+"_PMI_t4_2.txt"));
					break;
			}
		}
		
		private function loadDaanCom(e:Event):void
		{
			var l:URLLoader = e.currentTarget as URLLoader;
			l.removeEventListener(Event.COMPLETE,loadToolInfoCom);
		}
		
		private function loadDaanErr(e:IOErrorEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var l:URLLoader = e.currentTarget as URLLoader;
			l.removeEventListener(Event.COMPLETE,loadToolInfoCom);
			
			var btn:MovieClip = panel.btn_daan as MovieClip;
			btn.mouseChildren = false;
			btn.mouseEnabled = false;
			btn.filters = [FilterManager.getGrayFilter()];
			btn.removeEventListener(MouseEvent.CLICK,showDaan);
		}
		
		private function checkInList(param:String,arr:Array):Boolean
		{
			var b:Boolean = false;
			var i:uint = 0;
			for(i;i<arr.length;i++)
			{
				if(param == arr[i])
					b = true;
			}
			return b;
		}
		
		private function toolClick(e:MouseEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			panel.panel_duyin.visible = false;
			panel.panel_tool.visible = true;
			
			var target:MovieClip = e.currentTarget as MovieClip;
			target.gotoAndStop(2);
//			panel.panel_tool.show.show.gotoAndStop(target.name);
			panel.panel_tool.title.gotoAndStop(target.name);
			videoPlayer.stop();
			videoPlayer.closeVideoPlayer(1);
			
			var l:URLLoader = new URLLoader();
			l.addEventListener(Event.COMPLETE,loadToolInfoCom);
			l.load(new URLRequest(URL+target.name+".txt"));
			
			var i:uint = 0;
			for(i;i<6;i++)
			{
				if(i != target.name.split("_")[1])
				{
					panel["tool_"+i].gotoAndStop(1);
				}
			}
			
			i = 0;
			for(i;i<allSheng.length;i++)
			{
				if(!checkInList(allSheng[i],importantSheng))
				{
					panel["s_"+i].gotoAndStop(2);
					panel["s_"+i].alpha = 0.5;
				}else
				{
					panel["s_"+i].gotoAndStop(1);
				}
			}
			i = 0;
			for(i;i<allYun.length;i++)
			{
				if(!checkInList(allYun[i],importantYun))
				{
					panel["y_"+i].gotoAndStop(2);
					panel["y_"+i].alpha = 0.5;
				}else
				{
					panel["y_"+i].gotoAndStop(1);
				}
			}
		}
		
		private function loadToolInfoCom(e:Event):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var l:URLLoader = e.currentTarget as URLLoader;
			l.removeEventListener(Event.COMPLETE,loadToolInfoCom);
			var data:Object = l.data;
			var str:String = data.toString();
			
			panel.panel_tool.show.show.txt.htmlText = str;
			panel.panel_tool.show.show.txt.embedFonts = true;
			trace("length of txt:"+panel.panel_tool.show.show.txt.height);
			
			_uiScorllBar = createScrollBar(panel.panel_tool);
		}
		
		/**
		 * 清除面板事件
		 * @param $type
		 */
		private function clearPanelListener($type:uint):void
		{
			var panel:MovieClip = main["panel_"+$type];
			switch($type)
			{
				case 0:
					videoPlayer.stop();
					videoPlayer.closeVideoPlayer(1);
					if(_uiScorllBar)
					{
						_uiScorllBar.removeEventListener(MouseEvent.MOUSE_MOVE,scrollMove);
						_uiScorllBar.destroy();
						_uiScorllBar = null;
					}
					var i:uint = 0;
					for(i;i<allSheng.length;i++)
					{
						if(checkInList(allSheng[i],importantSheng))
						{
							panel["s_"+i].removeEventListener(MouseEvent.CLICK,shengOrYunClick);
						}
					}
					i = 0;
					for(i;i<allYun.length;i++)
					{
						if(checkInList(allYun[i],importantYun))
						{
							panel["y_"+i].removeEventListener(MouseEvent.CLICK,shengOrYunClick);
						}
					}
					break;
				case 1:
					break;
				case 2:
					panel.btn_important.removeEventListener(MouseEvent.CLICK,showImportant);
					panel.voice.bar.removeEventListener(MouseEvent.MOUSE_DOWN,voiceDown);
					stage.removeEventListener(MouseEvent.MOUSE_UP,voiceUp);
					stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
					break;
				case 3:
					break;
			}
			SoundManager.destroy();
			if(_uiScorllBar)
			{
				_uiScorllBar.removeEventListener(MouseEvent.MOUSE_MOVE,scrollMove);
				_uiScorllBar.destroy();
				_uiScorllBar = null;
			}
		}
		
		private function zhenrenOrZuixing(e:MouseEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var target:MovieClip = e.currentTarget as MovieClip;
			if(target.name == "btn_zhenren")
			{
//				panel.panel_duyin.btn_zhenren.gotoAndStop(2);
//				panel.panel_duyin.btn_zuixing.gotoAndStop(1);
				if(selectedImportant != "")
				{
					videoPlayer.load(URL+selectedImportant+"_zr.flv");
					videoPlayer.play();
				}
			}else if(target.name == "btn_zuixing")
			{
//				panel.panel_duyin.btn_zuixing.gotoAndStop(2);
//				panel.panel_duyin.btn_zhenren.gotoAndStop(1);
				if(selectedImportant != "")
				{
					videoPlayer.load(URL+selectedImportant+"_zx.flv");
					videoPlayer.play();
				}
			}
		}
		
		/**
		 * 显示练习内容
		 * @param e
		 */
		private function showLianxi(e:MouseEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			panel.btn_lianxi.gotoAndStop(2);
			panel.btn_daan.gotoAndStop(1);
			panel.show.show.y = 0;
			
//			var l:Loader = new Loader();
//			l.contentLoaderInfo.addEventListener(Event.COMPLETE,loadLianxiCom);
//			l.load(new URLRequest(URL+"PMI_t4_1.swf"));
			
			var l:URLLoader = new URLLoader();
			l.addEventListener(Event.COMPLETE,loadLianxiCom);
			l.load(new URLRequest(URL+LessonInfo.lessonID+"_PMI_t4_1.txt"));
		}
		
		private function showDaan(e:MouseEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			panel.btn_lianxi.gotoAndStop(1);
			panel.btn_daan.gotoAndStop(2);
			panel.show.show.y = 0;
			
//			var l:Loader = new Loader();
//			l.contentLoaderInfo.addEventListener(Event.COMPLETE,loadLianxiCom);
//			l.load(new URLRequest(URL+"PMI_t4_2.swf"));

			var l:URLLoader = new URLLoader();
			l.addEventListener(Event.COMPLETE,loadLianxiCom);
			l.load(new URLRequest(URL+LessonInfo.lessonID+"_PMI_t4_2.txt"));
		}
		
		private function loadLianxiCom(e:Event):void
		{
//			var panel:MovieClip = main["panel_"+now_type];
//			var show:MovieClip = panel.show.show as MovieClip;
//			while(show.numChildren>0) show.removeChildAt(0);
//			show.addChild(e.currentTarget.loader);
//			_uiScorllBar = createScrollBar(panel);

			var panel:MovieClip = main["panel_"+now_type];
			var l:URLLoader = e.currentTarget as URLLoader;
			l.removeEventListener(Event.COMPLETE,loadToolInfoCom);
			var data:Object = l.data;
			var str:String = data.toString();
			panel.show.show.txt.htmlText = str;
			panel.show.show.txt.embedFonts = true;
//			setMyTextFormat(panel.show.show.txt,"MYuenHK-Medium");
			trace("txtHeight of txt:"+panel.show.show.txt.textHeight)
			trace("numLines of txt:"+panel.show.show.txt.numLines)
			trace("length of txt:"+panel.show.show.txt.height)
			
			_uiScorllBar = createScrollBar(panel);
		}
		
		private function voiceDown(e:MouseEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var target:MovieClip = e.currentTarget as MovieClip;
			var rec:Rectangle = new Rectangle(0,0,panel.voice.bg.width,0);
			target.startDrag(true,rec);
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
		}
		
		private function mouseMove(e:MouseEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var volume:Number = panel.voice.bar.x/panel.voice.bg.width;
			SoundManager.volume = volume;
			trace("volume change:"+volume);
		}
		
		private function voiceUp(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
			var panel:MovieClip = main["panel_"+now_type];
			panel.voice.bar.stopDrag();
		}
		
		/**
		 * 儿歌显示内容下载完成
		 * @param e
		 */
		private function loadErgeCom(e:Event):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var l:URLLoader = e.currentTarget as URLLoader;
			l.removeEventListener(Event.COMPLETE,loadToolInfoCom);
			var data:Object = l.data;
			var str:String = data.toString();
			panel.show.show.txt.htmlText = str;
			panel.show.show.txt.embedFonts = true;
			
//			var show:MovieClip = panel.show.show as MovieClip;
//			while(show.numChildren>0) show.removeChildAt(0);
//			show.addChild(e.currentTarget.loader);
			
			_uiScorllBar = createScrollBar(panel);
		}
		
		/**
		 * 播放兒歌
		 * @param e
		 */
		private function playErge(e:MouseEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var volume:Number = panel.voice.bar.x/panel.voice.bg.width;
			SoundManager.play(URL+LessonInfo.lessonID+"_PMI_t3.mp3");
			SoundManager.volume = volume;
		}
		
		/**
		 * 停止播放兒歌
		 * @param e
		 */
		private function stopErge(e:MouseEvent):void
		{
			SoundManager.stop();
		}
		
		/**
		 * 显示重要字词
		 * @param e
		 */
		private function showImportant(e:MouseEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var show:MovieClip = panel.show.show as MovieClip;
			
			var l:URLLoader = new URLLoader();
			l.addEventListener(Event.COMPLETE,loadErgeCom);
			if(ergeShowBool)
			{
				l.load(new URLRequest(URL+LessonInfo.lessonID+"_PMI_t3_1.txt"));
			}else
			{
				l.load(new URLRequest(URL+LessonInfo.lessonID+"_PMI_t3_2.txt"));
			}
			ergeShowBool = !ergeShowBool;
		}
		
		/**
		 * 選擇拼讀或視讀
		 * @param e
		 */
		private var checkHash:Dictionary;
		private function shiOrPinClick(e:MouseEvent):void
		{
			var target:MovieClip = e.currentTarget as MovieClip;
			if(target.currentFrame == 1)
			{
				var p:MovieClip = target.parent as MovieClip;
				p.status = target.name.split("_")[1];
				target.gotoAndStop(2);
				
				if(p.status == "s")
				{
					p["btn_p"].gotoAndStop(1);
				}else
				{
					p["btn_s"].gotoAndStop(1);
				}
				checkHash = new Dictionary();
				checkPinduExist(p.name,p.status);
			}
		}
		
		private function checkPinduExist($name:String,status:String):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var show:MovieClip = panel.show.show;
			
			var itemShow:Item_Important = show.getChildByName($name) as Item_Important;
			if(itemShow)
			{
				if(!SoundManager.ownEvent.hasEventListener(Event.COMPLETE))
					SoundManager.ownEvent.addEventListener(Event.COMPLETE,checkCom);
				var lword:Array = LessonInfo.getInfoByLevelID($name).lessonWord;
				var j:uint = 0;
				for(j;j<3;j++)
				{
					var lett:String = lword[j];
					if(lett)
					{
						var target:MovieClip = itemShow["lesson_"+j];
						var obj:Object = LetterInfo.getInfoByLevelID(lett);
						var _url:String;
						if(obj.checkID == "")
							_url = URL+lett+"_"+status+".mp3";
						else
							_url = URL+obj.checkID+"_"+status+".mp3";
						checkHash[_url] = target;
						SoundManager.checkMp3Exist(_url);
					}
				}
				
				var oword:Array = LessonInfo.getInfoByLevelID($name).otherWord;
				j=0
				for(j;j<3;j++)
				{
					var let:String = oword[j];
					if(let)
					{
						var target1:MovieClip = itemShow["other_"+j];
						var obj1:Object = LetterInfo.getInfoByLevelID(let);
						var _url1:String;
						if(obj1.checkID == "")
							_url1 = URL+let+"_"+status+".mp3";
						else
							_url1 = URL+obj1.checkID+"_"+status+".mp3";
						checkHash[_url1] = target1;
						SoundManager.checkMp3Exist(_url1);
					}
				}
			}
		}
		
		private function checkCom(e:EventAnj):void
		{
			var data:Object = e.data;
			var flag:uint = data.flag;
			var url:String = data.url;
			var target:MovieClip = checkHash[url] as MovieClip;
			delete checkHash[url];
			
			if(target)
			{
				if(flag == 0)
				{
					target.mouseChildren = false;
					target.mouseEnabled = false;
					target.filters = [FilterManager.getGrayFilter()];
				}else
				{
					target.mouseChildren = true;
					target.mouseEnabled = true;
					target.filters = [];
				}
			}
		}
		
		/**
		 * 点击字词
		 * @param e
		 */
		private function wordClick(e:MouseEvent):void
		{
			var target:MovieClip = e.currentTarget as MovieClip;
			
			var index:uint = target.name.split("_")[1];
			var word:Array;
			var pin:String;
			if(target.name.split("_")[0] == "lesson")
			{
				word = LessonInfo.getInfoByLevelID(target.parent.name).lessonWord;
			}else
			{
				word = LessonInfo.getInfoByLevelID(target.parent.name).otherWord;
			}
			pin = word[index];
			
			var p:MovieClip = target.parent as MovieClip;
			var status:String = p.status;
			var obj:Object = LetterInfo.getInfoByLevelID(pin);
			var _url:String;
			if(obj.checkID == "")
				_url = URL+pin+"_"+status+".mp3";
			else
				_url = URL+obj.checkID+"_"+status+".mp3";
			trace("start play sound:" + _url);
			SoundManager.play(_url);
		}
		
		/**
		 * 点击重要声母或韵母
		 * @param e
		 */
		private function shengOrYunClick(e:MouseEvent):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var target:MovieClip = e.currentTarget as MovieClip;
			var index:uint = target.name.split("_")[1];
			
			panel.panel_duyin.visible = true;
			panel.panel_tool.visible = false;
			
			var oldIndex:uint;
			if(allSheng.indexOf(selectedImportant) != -1)
			{
				oldIndex = allSheng.indexOf(selectedImportant);
				panel["s_"+oldIndex].gotoAndStop(1);
			}else if(allYun.indexOf(selectedImportant) != -1)
			{
				oldIndex = allYun.indexOf(selectedImportant);
				panel["y_"+oldIndex].gotoAndStop(1);
			}
			
			if(target.name.split("_")[0] == "s")
			{
				selectedImportant = allSheng[index];
				panel.panel_duyin.tip.visible = true;
				panel.panel_duyin.tip.gotoAndStop(1);
			}else
			{
				selectedImportant = allYun[index];
				panel.panel_duyin.tip.visible = true;
				panel.panel_duyin.tip.gotoAndStop(2);
			}
			
//			if(panel.panel_duyin.btn_zhenren.currentFrame == 2 && target.currentFrame == 1)
//			{
//				videoPlayer.load(URL+selectedImportant+"_zr.flv");
//				videoPlayer.play();
//				trace("play video:" + URL+selectedImportant+"_zr.flv")
//			}else if(panel.panel_duyin.btn_zuixing.currentFrame == 2 && target.currentFrame == 1)
//			{
//				videoPlayer.load(URL+selectedImportant+"_zx.flv");
//				videoPlayer.play();
//				trace("play video:" + URL+selectedImportant+"_zx.flv")
//			}
			initYunTips();
			
			if(target.currentFrame != 2)
			{
				if(allSheng.indexOf(selectedImportant) != -1)
				{
					panel["s_"+allSheng.indexOf(selectedImportant)].gotoAndStop(1);
				}else if(allYun.indexOf(selectedImportant) != -1)
				{
					panel["y_"+allYun.indexOf(selectedImportant)].gotoAndStop(1);
				}
				target.gotoAndStop(2);
			}
			
			var i:uint = 0;
			for(i;i<6;i++)
			{
				panel["tool_"+i].gotoAndStop(1);
			}
		}
		
		/**
		 * 初始化重點韵母內容提示
		 */
		private function initYunTips():void
		{
			var panel:MovieClip = main["panel_"+now_type];
			
			panel.panel_duyin.spell.text = selectedImportant;
			setMyTextFormat(panel.panel_duyin.spell,"PinYinok",58);
			panel.panel_duyin.show.show.txt.text = "";
			panel.panel_duyin.show.show.gotoAndStop(1);
			
			if(selectedImportant == "") return;
			var l:URLLoader = new URLLoader();
			l.addEventListener(Event.COMPLETE,loadLatterInfoCom);
			
			var url:String = URL+String(LessonInfo.lessonID.split(".")[0])+"_"+selectedImportant+".txt";
			l.load(new URLRequest(url));
		}
		
		/**
		 * 加载重点韵母说明完毕
		 * @param e
		 */
		private function loadLatterInfoCom(e:Event):void
		{
			var panel:MovieClip = main["panel_"+now_type];
			var l:URLLoader = e.currentTarget as URLLoader;
			l.removeEventListener(Event.COMPLETE,loadLatterInfoCom);
			var data:Object = l.data;
			var str:String = data.toString();
			
			panel.panel_duyin.show.show.txt.htmlText = str;
			panel.panel_duyin.show.show.txt.embedFonts = true;
			_uiScorllBar = createScrollBar(panel.panel_duyin);
		}
		
		/**
		 * 創建滾動條
		 * @param panel
		 * @return 
		 */
		private function createScrollBar(panel:MovieClip):UIScrollBar
		{
			if(_uiScorllBar)
			{
				_uiScorllBar.removeEventListener(MouseEvent.MOUSE_MOVE,scrollMove);
				_uiScorllBar.destroy();
				_uiScorllBar = null;
			}
			
			panel.show.show.y = 0;
			var maxLenght:Number = panel.show.mask.height;
			
			var txt:TextField = panel.show.show.getChildByName("txt");
			if(txt)
			{
				txt.height = txt.textHeight + 5;
			}
			var totalLenght:Number = panel.show.show.height;
			
			if(totalLenght<maxLenght)
			{
				panel.scrollBar.barBall.visible = false;
				panel.scrollBar.barBg.visible = false;
				panel.scrollBar.upBtn.visible = false;
				panel.scrollBar.downBtn.visible = false;
			}else
			{
				panel.scrollBar.barBall.visible = true;
				panel.scrollBar.barBg.visible = true;
				panel.scrollBar.upBtn.visible = true;
				panel.scrollBar.downBtn.visible = true;
			}
			var bar:UIScrollBar = new UIScrollBar(panel.scrollBar.barBall,panel.scrollBar.barBg,maxLenght,panel.scrollBar.upBtn,panel.scrollBar.downBtn);
			bar.wheelObject = panel.show;
			bar.addEventListener(MouseEvent.MOUSE_MOVE,scrollMove);
			bar.totalLength = totalLenght + 15;
			return bar;
		}
		
		private function setMyTextFormat(txt:TextField,_font:String,_size:Object=24):void
		{
			var txtFormat:TextFormat = new TextFormat();
			txtFormat.font = _font;   //设定字体
			txtFormat.size = _size;    //设定大小
			txtFormat.bold = true;    //粗体字
			txt.embedFonts = true;
			txt.setTextFormat(txtFormat);
		}
		
		/**
		 * 移動滾動條
		 * @param e
		 */
		private function scrollMove(e:Event):void
		{
			var index:int = _uiScorllBar.index;
			trace(index);
			var panel:MovieClip = main["panel_"+now_type];
			
			if(now_type == 0)
			{
				if(panel.panel_duyin.visible)
					panel = panel.panel_duyin;
				else
					panel = panel.panel_tool;
			}
			panel.show.show.y = -index;
		}
	}
}
